package com.pioneer.app.dev.osuser.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.exception.JDBCConnectionException;

import com.app.Const;
import com.app.util.MD5;
import com.opensymphony.xwork2.ActionSupport;

import config.HibernateSessionFactory;

public class LoginAction extends ActionSupport implements ServletRequestAware{
	private java.lang.String userName;
	private java.lang.String userPwd;
	private HttpServletRequest request;
	public java.lang.String getUserName() {
		return userName;
	}
	public void setUserName(java.lang.String userName) {
		this.userName = userName;
	}
	public java.lang.String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(java.lang.String userPwd) {
		if(null!=userPwd){
			this.userPwd=userPwd;
		}
//		this.userPwd = userPwd;
	}
	public String execute() throws Exception {
		String returnpage="";
		
		try {
			UserlistDAO uld=new UserlistDAO();
			
			if(AdminService.USER_NAME.equalsIgnoreCase(this.userName)){
				String scpwd=MD5.ToMD5(this.userPwd);
				if(AdminService.getInst().getPWD().equalsIgnoreCase(scpwd)){
					Userlist userobj= new Userlist();
					userobj.setId(new Integer(1));
					userobj.setUserName("sysadmin");
					request.getSession().setAttribute(Const.SESSION_USER_OBJECT, userobj);
					returnpage="success";
					return returnpage;
				}
			}else{
				List list=uld.getByUsername(this.getUserName());
				this.userPwd=MD5.ToMD5(userPwd);
				for(int i=0;i<list.size();i++)   
				{
					    Userlist user = (Userlist)list.get(i);   
				            String username=user.getUserName();
				            String password=user.getUserPwd();
				        if(!username.equals(this.getUserName())||!password.equals(this.getUserPwd())||"".equals(this.getUserName())||"".equals(this.getUserPwd()))
				        {
				        	returnpage="failer";
				        }
				        else if(username.equals(this.getUserName())&&password.equals(this.getUserPwd()))
								{
									request.getSession().setAttribute(Const.SESSION_USER_OBJECT, user);
									returnpage="success";
									return returnpage;
								}
								else
								{									
									returnpage="failer";			
								}
				}
			}
			
		}catch (JDBCConnectionException re) {
			HibernateSessionFactory.reconnection();
			throw re;
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		this.addFieldError("username","用户名或密码错误！");
		return "failer";
	}
	
	@Override
	public void validate() {
		if("".equals(this.getUserName())||"".equals(this.getUserPwd()))
		{
			this.addFieldError("userName", "用户名或密码不能为空！");
		}		
	}
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request=arg0;
	}
	
	/*public String execute() throws Exception {
		if("admin".equals(this.getUserName())&&"admin".equals(this.getUserPwd()))
		{
			return "success";
		}
		else
		{
			this.addFieldError("userName", "username or password is wrong");
			return "failer";
		}
	}*/
	

 
}
